package others;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ParseContent {

    public static void main(String[] args) throws IOException {
//        String content = ""
        try (FileInputStream fr = new FileInputStream("D:\\data\\govcg\\all_content.txt")){
            byte[] bytes = fr.readAllBytes();
            String contents = new String(bytes);
            Map<String, String> idAndContentMap = new HashMap<>();
            String id = "";
            StringBuilder contentBuilder = new StringBuilder();
            String[] allLines = contents.split("\n");
            for (int i = 0; i < allLines.length; i++) {
                String line = allLines[i];
                if (line.startsWith("===id:")) {
                    // 上一层关闭
                    if (contentBuilder.length() > 0) {
                        idAndContentMap.put(id, contentBuilder.toString());
                        contentBuilder = new StringBuilder();
                    }
                    id = line.split(":")[1].replace("\r", "");
                } else if (line.startsWith("====")) {
                } else {
                    contentBuilder.append(line);
                }
            }
            idAndContentMap.put(id, contentBuilder.toString());
            // 解析数据
            for (Map.Entry<String, String> entry : idAndContentMap.entrySet()) {
                Document document = Jsoup.parse(entry.getValue());
                Elements table = document.body().select("table");
                Elements trs = table.select("tr");
                if (trs.size() > 1) {
                    for (int i = 1; i < trs.size(); i++) {
//                        String title = trs.get(i).select("td").get(1).text();
                        String time = trs.get(i).select("td").get(5).text();
                        String key = entry.getKey();
                        System.out.println(time);
//                        System.out.println(key + "\t" + title + "\t" + budge );
                    }
                }
            }
        }
    }

}
